// pages/indexDetails/indexDetails.js
var indexData = require("../indexDetailsdata/indexDetailsdata.js");
var num = 0;
var sortRule1 = true; // 价格
var sortRule2 = true; // 销量
var sortClick1 = 2;
var sortClick2 = 2;
var twoBtn = false;//单列多列
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classifHad:true,//是否有分类
    classif: [//分类
      {
        "text": "巴氏鲜奶",
        "id":"1",
        "classifBtn":true
      },
      {
        "text": "酸奶",
        "id": "2",
        "classifBtn": false
      },
      {
        "text": "发酵乳",
        "id": "3",
        "classifBtn": false
      },
      {
        "text": "其他商品",
        "id": "4",
        "classifBtn": false
      }
    ],
    flashSalebg: {
      "flashSalebg1": "http://www.tianshangding.cn/font/img/countDownR1.png",
      "flashSalebg2": "http://www.tianshangding.cn/font/img/countDownR2.png",
      "flashSalebg3": "http://www.tianshangding.cn/font/img/countDownR3.png"
    },
    sortClick1: 1,
    sortClick2: 1,
    twoBtn: false,
    sortImg: {
      "sort1": "../images/index/sort1.png",
      "sort2": "../images/index/sort2.png",
      "sort3": "../images/index/sort3.png",
      "two": "../images/index/two.png",//两列
      "one": "../images/index/one.png"//单行
    },
    usedata: "",//产品列表信息
    usedata2: "",//产品列表信息
    navbarInitTop: 0, //导航栏初始化距顶部的距离
    isFixedTop: false, //是否固定顶部
    flag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      //推荐消息， 订奶列表数据
      usedata: indexData.setdata,
      messageRolling: indexData.messageRolling
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    if (that.data.navbarInitTop == 0) {
      //获取节点距离顶部的距离
      wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
        if (rect && rect.top > 0) {
          var navbarInitTop = parseInt(rect.top);
          that.setData({
            navbarInitTop: navbarInitTop
          });
        }
      }).exec();
    }
  },
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
    //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
    //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    if (that.data.isFixedTop === isSatisfy) {
      return false;
    }
    that.setData({
      isFixedTop: isSatisfy,
      flag: isSatisfy
    });
  },
  mySort1: function (e) {
    // 价格
    //property 根据什么排序
    var property = e.currentTarget.dataset.property;
    var self = this;
    var arr = self.data.usedata;
    sortRule2 = true;
    sortClick2 = 1;
    this.setData({
      sortClick2: sortClick2
    })
    sortClick2 = 2;
    if (sortClick1 == 1) {
      sortRule1 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
      sortRule1 = true;
    } else if (sortClick1 == 2) {
      sortRule1 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
    } else if (sortClick1 == 3) {
      sortRule1 = false;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
    }
    this.setData({
      sortClick1: sortClick1
    })
    console.log(sortClick1)
    sortClick1++;
    if (sortClick1 > 3) {
      sortClick1 = 1;
    }
    // 点击排序
    var that = this;
    var isFixedTop = that.data.isFixedTop;//isFixedTop是一个开关变量，用来控制点击滑动到指定位置
    var query = wx.createSelectorQuery(); //获取某个元素返回的是一个对象
    if (isFixedTop) {
      var _heightd = this.data.navbarInitTop;
      wx.pageScrollTo({
        scrollTop: _heightd,   //页面滚动的距离
        duration: 300,    //页面滚动速度 单位ms
        success: function (e) {    //成功函数
          that.setData({
            flag: false
          })
        }
      });
    }
  },
  mySort2: function (e) {
    // 销量
    //property 根据什么排序
    var property = e.currentTarget.dataset.property;
    var self = this;
    var arr = self.data.usedata;
    sortRule1 = true;
    sortClick1 = 1;
    this.setData({
      sortClick1: sortClick1
    })
    sortClick1 = 2;
    if (sortClick2 == 1) {
      sortRule2 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
      sortRule2 = true;
    } else if (sortClick2 == 2) {
      sortRule2 = false;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
    } else if (sortClick2 == 3) {
      sortRule2 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
    }
    this.setData({
      sortClick2: sortClick2
    })
    sortClick2++;
    if (sortClick2 > 3) {
      sortClick2 = 1;
    }
    console.log(sortClick2)
    // 点击排序
    var that = this;
    var isFixedTop = that.data.isFixedTop;//isFixedTop是一个开关变量，用来控制点击滑动到指定位置
    var query = wx.createSelectorQuery(); //获取某个元素返回的是一个对象
    if (isFixedTop) {
      var _heightd = this.data.navbarInitTop;
      wx.pageScrollTo({
        scrollTop: _heightd,   //页面滚动的距离
        duration: 300,    //页面滚动速度 单位ms
        success: function (e) {    //成功函数
          that.setData({
            flag: false
          })
        }
      });
    }

  },
  compare: function (property, bol) {
    // 排序计算
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      if (bol) {
        return value1 - value2;
      } else {
        return value2 - value1;
      }
    }

  },
  twoBtn: function () {
    twoBtn = !twoBtn;
    this.setData({
      twoBtn: twoBtn
    })
  },
  goClassif: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
    wx.navigateTo({
      url: '../indexDetails/indexDetails?newsid=' + newsid
      // 跳转到其他分类
    })
  },
  goDetails: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
    wx.navigateTo({
      url: '../details/details?newsid=' + newsid
      // 跳转到商品详情
    })
  },
  onShareAppMessage: function () {//转发
    return {
      title: '弹出分享时显示的分享标题',
      desc: '分享页面的内容',
      path: '/pages/indexDetails/indexDetails' // 路径，传递参数到指定页面。
    }
  }
})